<template>

  <el-dialog title="新增车辆" :visible="showDialog" @close="close">
    <el-form ref="addForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item prop="truckTypeId" label="车辆类型">
        <el-select v-model="form.truckTypeId" placeholder="请输入车辆类型" style="width: 80%">
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="licensePlate" label="车牌号码">
        <el-input v-model="form.licensePlate" placeholder="请输入车牌号码" style="width: 80%" />
      </el-form-item>
      <el-form-item prop="deviceGpsId" label="GPS设备ID">
        <el-input v-model="form.deviceGpsId" placeholder="请输入GPS设备ID" style="width: 80%" />
      </el-form-item>

      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="submitForm">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>

import { addCarList } from '../../api/carList'
import { getcarTypeList } from '../../api/carType'
export default {
  props: {
    carType: {
      type: Array,
      default: () => []
    },
    showDialog: {
      type: Boolean,
      default: false
    }

  },
  data() {
    return {
      typeList: [],
      form: {
        licensePlate: '', // 车牌号码
        deviceGpsId: '', // GPS
        truckTypeId: null // 类型id
      },
      rules: {
        licensePlate: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { pattern: /^[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}$/, message: '请输入正确的车牌号', trigger: 'blur' }
        ],
        deviceGpsId: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ],
        truckTypeId: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    this.getcarTypeList()
  },
  methods: {
    // 车型信息列表
    async getcarTypeList() {
      const res = await getcarTypeList()
      this.typeList = res.data
      console.log(this.typeList)
    },
    // 关闭弹窗
    close() {
      this.$emit('update:showDialog', false)
      this.$refs.addForm.resetFields()
    },

    async submitForm() {
      await this.$refs.addForm.validate()
      await addCarList(this.form)
      this.$message.success('新增车辆成功')
      // 通知父组件更新
      this.$emit('updateCarType')
      this.close()
    }
  }

}
</script>

<style>

</style>
